﻿var gridManipRowPos = [];
var gridManipColPos = [];
var gridManipRowName = [];
var gridManipColName = [];
var distFunc = distanceV35;
var gridHalved = false;

function isLocked() {
    return $('.gridmanip-cell').hasClass('grid-locked');
}
function initGridManipulator() {
    var $cell = $('.gridmanip-cell');
    $('.gridcommand').hide();
    $cell.hide();
    $('.distance').hide();

    $('body').unbind('click').bind('click', function () {
        var $cell = $('.gridmanip-cell');
        if ($cell.hasClass('grid-locking')) $cell.removeClass('grid-locking');
        else if (isLocked()) $cell.removeClass('grid-locked').hide();
    });
    var $outer = $('.gridmanip');
    var $inner = $('.gridmanip-inner');
    $outer.mouseenter(function () { $cell.show() })
        .mouseleave(function () {
            if (!isLocked()) {
                $cell.hide();
                $('.distance').hide();
            }
        })
        .mousemove(function (e) {
            if (isLocked()) return;

            $cell.show();
            var offset = $inner.offset();
            var x = e.pageX - offset.left;
            var y = e.pageY - offset.top;
            var col = 0;
            while (col < gridManipColPos.length - 1 && gridManipColPos[col + 1] <= x) col++;
            var row = 0;
            while (row < gridManipRowPos.length - 1 && gridManipRowPos[row + 1] <= y) row++;
            var cellName = gridManipColName[col] + gridManipRowName[row];
            $cell.attr('title', cellName)
                .css("left", gridManipColPos[col] + "px").css("top", gridManipRowPos[row] + "px");
            $('.gridcommand').removeClass('grid-current-command').hide();
            var $active = $('.gridcommand.allcells').add('.gridcommand.cell-' + cellName.replace(/\+/g, '-'));
            $active.addClass('grid-current-command').show();
            $('.gridcommand-active-cell').val(cellName);
            $('.active-cell').html(cellName);
            var i = 0;
            $('.distance').each(function () {
                var x = parseInt($('.x', this).html());
                var y = parseInt($('.y', this).html());
                var s = parseInt($('.s', this).html());
                $('.dist', this).html(getDistance(row, col, x, y, s));
            }).show();
            if ($active.length > 1) {
                $cell.addClass('grid-multi-commands').removeClass('grid-one-command');
            } else if ($active.length == 1) {
                $cell.addClass('grid-one-command').removeClass('grid-multi-commands');
            } else {
                $cell.removeClass('grid-one-command').removeClass('grid-multi-commands');
            }
        });
    $('.gridmanip-cellinner').click(function () {
        var $c = $('.gridmanip-cell');
        if ($c.hasClass('grid-multi-commands')) {
            $c.addClass('grid-locked').addClass('grid-locking');
        }
    });
}
function getDistance(row, col, x, y, s) {
    var x1 = 1 + (gridHalved ? col / 2 : col);
    var y1 = 1 + (gridHalved ? row / 2 : row);
    var distance = null;
    var x2s = x + (gridHalved ? 0.5 : 0);
    var y2s = y + (gridHalved ? 0.5 : 0);
    if (x1 >= x2s && x1 <= x2s + s - 1 && y1 >= y2s && y1 <= y2s + s - 1) return 0;
    var inc = gridHalved ? 0.5 : 1;
    for (var xinc = 0; xinc < s - 0.5; xinc += inc) {
        for (var yinc = 0; yinc < s - 0.5; yinc += inc) {
            var x2 = x2s + xinc;
            var y2 = y2s + yinc;
            var dist = distFunc(x1 - x2, y1 - y2);
            if (distance == null || dist < distance) distance = dist;
        }
    }
    return distance;
}
function distanceV35(dx, dy) {
    return (Math.round(Math.sqrt(dx * dx + dy * dy) * 500) / 100);
}
function distanceV40(dx, dy) {
    return Math.max(Math.abs(dx), Math.abs(dy));
}
function initColorPicker(textboxId, placeHolderId) {
    var $holder = $('#' + placeHolderId);
    var $textbox = $('#' + textboxId);
    $holder.hide();
    $holder.farbtastic('#' + textboxId);
    $textbox.focus(function () { $holder.show('fast') })
        .blur(function () { $holder.hide('fast') });
}
